<?php
class FoodAction extends CommonAction {

    public function index()
    {
        $food = M('Food');
        parent::page($food->count());
        $data = $food->order('add_time desc')
                     ->limit($this->limit)
                     ->select();

        foreach($data as $k=>&$v)
        {
            $v['main_food'] = $this->_formatToStr($v['food_main_part']);
            $v['sur_food'] = $this->_formatToStr($v['food_sur_part']);;
        }

        $this->assign('data', $data);
        $this->display();
    }

    public function add()
    {
        $this->_getMainAndSur();

        $food = M('Food');
        if(isset($_POST['send']))
        {
            $this->validate(self::$ADD_SEC);

            $data['food_name'] = $this->_post('food_name');
            $data['food_type'] = $this->_post('food_type');
            $data['food_main_part'] = $this->_formatFoodPart('main');
            $data['food_sur_part'] = $this->_formatFoodPart('sur');
            $data['food_price_count'] = $this->_post('food_price_count');
            $data['add_time'] = getDateTime();

            if($food->add($data))
            {
                $content = "用户：".session('admin')." 添加了一个菜式种类：$data[food_name]";
                addAdminRecord($content);
                redirectByModal('添加菜式成功!', self::$SUCCESS, __URL__ . '/index');
            }
        }

        $this->display();
    }

    public function update($id)
    {
        $this->_getMainAndSur();

        $food = M('Food');
        $currentData = $food->where('id='.$id)->find();

        $mainPart = unserialize($currentData['food_main_part']);
        $surPart = unserialize($currentData['food_sur_part']);

        if(isset($_POST['send']))
        {
            $this->validate(self::$UPDATE_SEC);

            $data['food_name'] = $this->_post('food_name');
            $data['food_type'] = $this->_post('food_type');
            $data['food_main_part'] = $this->_formatFoodPart('main');
            $data['food_sur_part'] = $this->_formatFoodPart('sur');
            $data['food_price_count'] = $this->_post('food_price_count');
            $data['id'] = $this->_post('id');

            $food->save($data);
            $content = "用户：".session('admin')." 修改了一个供货种类：$currentData[food_name]";
            addAdminRecord($content);
            redirectByModal('修改菜式成功!', self::$SUCCESS, __URL__.'/index');
        }
        $this->assign(
            array(
                'data'=>$currentData,
                'main_part'=>$mainPart,
                'sur_part'=>$surPart,
        ));
        $this->display();
    }

    public function delete()
    {
        $id = $this->_post('id');
        $food = M('Food');
        $data = $food->field('food_name')->where('id='.$id)->find();

        if($food->where('id='.$id)->delete())
        {
            $content = "用户：".session('admin')." 删除了一个菜式种类：$data[food_name]";
            addAdminRecord($content);
            redirectByModal('删除菜式成功!', self::$SUCCESS, __URL__ . '/index');
        }
    }

    // 验证
    private function validate($sec)
    {
        $food_name = $this->_post('food_name');
        $food_type = $this->_post('food_type');
        $main_good = $this->_post('main_good');
        $sur_good = $this->_post('sur_good');

        if($food_name == '')
            redirectByModal('菜品名称必须填写!', self::$ERROR);
        else if(sizeof($main_good) == 1 && $main_good[0] == -1)
            redirectByModal('菜品主料必须填写!', self::$ERROR);
        else if(mb_strlen($food_name, 'utf-8') > 30)
            redirectByModal('菜品名称不能超过30位!', self::$ERROR);
        else if($food_type == -1)
            redirectByModal('菜品类型必须选择!', self::$ERROR);
    }

    // 获取单位
    public function getUnitAndPrice()
    {
        $id = $this->_post('id');
        $stuff = M('Stuff');
        $data = $stuff->field(array('stuff_unit', 'stuff_price'))->where('id='.$id)->find();

        echo json_encode($data);
    }

    private function _formatFoodPart($type)
    {
        $good = $this->_post($type.'_good');
        $num = $this->_post($type.'_num');
        $unit = $this->_post($type.'_unit');
        $price = $this->_post($type.'_price');
        $temp = array();

        foreach($good as $k=>$v)
        {
            if($v != -1)
                $temp[] = array('food'=>$v, 'num'=>$num[$k], 'unit'=>$unit[$k], 'price'=>$price[$k]);
        }

        return serialize($temp);
    }

    private function _formatToStr($val)
    {
        $stuff = M('stuff');

        $good = unserialize($val);
        $str = '';

        foreach($good as $k2=>$v2)
        {
            $id = $v2['food'] . ' ';
            $stuffName = $stuff->field('stuff_name')->where('id='.$id)->find();
            $str .= $stuffName['stuff_name']. ' ';
            $str .= $v2['num'] . ' ';
            $str .= $v2['unit'] . ' ';
            $str .= '需 ' . $v2['price']*$v2['num']. ' 元';
            $str .= '<br />';
        }
        return $str == '' ? '无' : $str;
    }

    // 获取主辅料
    private function _getMainAndSur()
    {
        $stuff = M('Stuff');

        $mainGood = $stuff->field(array('id', 'stuff_name'))
            ->where("stuff_type='主料' or stuff_type='主料或辅料'")
            ->select();

        $surGood = $stuff->field(array('id', 'stuff_name'))
            ->where("stuff_type='辅料' or stuff_type='主料或辅料'")
            ->select();

        $this->assign(array(
            'mainGood' => $mainGood,
            'surGood' => $surGood
        ));
    }

}